let orderModel = require("../model/order")
let cartModel = require("../model/cart")
class Order {
    // [1,2,3].join() => 1,2,3
    /**
     * order_no
     * user_id
     * payable_amount 应付商品总金额
     * real_amount 实际支付金额
     * nowTime 当前时间
     * order_amount 订单总金额
     * 
     */
    static async createOrder(ctx) {
        let { cartIdArr } = ctx.request.body
        try {
            // 查询购物车表 
            let cartList = await cartModel.getCartIdInfo(cartIdArr)
            if (cartList.length == 0) {
                ctx.body = {
                    code: 201,
                    codeInfo: "参数有误，未找到购物车数据"
                }
            } else {
                // 计算各种金额
                let order_amount = 0
                for (var i = 0; i < cartList.length; i++) {
                    // 解决精度问题 保留2位小数的方法1. Decimal.js他的实现原理、Big.js 实现原理 2. toFixed()*1=>返回的是字符串 3.扩大倍数
                    order_amount += cartList[i].price * 1000 * cartList[i].num
                }

                order_amount = order_amount / 1000
                let payable_amount = order_amount
                let real_amount = order_amount
                let user_id = cartList[0].uid
                // 订单号 = 唯一（不能重复）生成规则 当前时间戳（毫秒）+2位随机数
                let order_no = "DD" + new Date().getTime() + ctx.rand(2)

                let nowTime = ctx.time.getFormatTimeFull(new Date().getTime())
                let orders = await orderModel.addOrder({ order_amount, payable_amount, real_amount, user_id, order_no, nowTime })
                ctx.body = orders
                let orderid = orders.insertId//insertId
                for (var i = 0; i < cartList.length; i++) {
                    await orderModel.createOrderGoods({ orderid, ...cartList[i] })
                }
                // 添加订单商品 完成后 删除相应的购物车数据
                await cartModel.del(cartIdArr)

                ctx.body = {
                    code: 200,
                    codeInfo: "订单创建成功",
                    order_no: order_no
                }
            }
        } catch (error) {
            ctx.body = {
                code: 202,
                codeInfo: "订单创建异常"
            }
        }
    }


    static async getOrderInSn(ctx) {
        let { order_no } = ctx.query
        let info = await orderModel.getSnOrder(order_no)
        if (info) {
            ctx.body = {
                code: 200,
                codeInfo: "成功",
                data: info[0]
            }
        } else {
            ctx.body = {
                code: 201,
                codeInfo: "订单号有误"
            }
        }
    }

    static async getOrderList(ctx) {
        let { uid, status, page } = ctx.request.body
        if (!uid) {
            ctx.body = {
                code: 201,
                codeInfo: "参数不足"
            }
        } else {

           
            uid = parseInt(uid)
            // 1 待付款 2 代消费 3 已完成 待评价 4 售后 
            status = status?parseInt(status):0


            page = page ? parseInt(page) : 1
            let list = await orderModel.getOrder(uid, status, page)
            ctx.body={
                code:200,
                codeInfo:"成功",
                data:list
            }
        }

    }
}
module.exports = Order